package com.loongxun.xxapp;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.loongxun.xxapp.ui.login.LoginActivity;
import com.loongxun.xxapp.ui.phone.PhoneBindingActivity;
import com.loongxun.xxapp.ui.user.AreaActivity;
import com.loongxun.xxapp.ui.user.MailListActivity;
import com.loongxun.xxapp.ui.user.SetActivity;
import com.loongxun.xxapp.utils.view.MyDialog;

public class MainActivity extends Activity implements View.OnClickListener{

    private SharedPreferences share;
    private SharedPreferences.Editor editor;
    private String user_id;

    private LinearLayout ll_sign_out;

    private MyDialog mDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        share = getSharedPreferences("userInfo", 0);
        editor = share.edit();//获取编辑器
        user_id = share.getString("user_id", "");

        if (isLogin(user_id)) {

        }
        findViewById(R.id.iv_user_img).setOnClickListener(this);
        findViewById(R.id.ll_call_transfer).setOnClickListener(this);
        findViewById(R.id.ll_mail_list).setOnClickListener(this);
        findViewById(R.id.ll_position_information).setOnClickListener(this);
        findViewById(R.id.ll_set_up).setOnClickListener(this);
        ll_sign_out = (LinearLayout) findViewById(R.id.ll_sign_out);
        ll_sign_out.setOnClickListener(this);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0) {
            if (data != null){

            }
        }
    }

    @Override
    public void onClick(View view) {
        Intent intent;
        switch (view.getId()){
            case R.id.iv_user_img:
                intent = new Intent(this, SetActivity.class);
                startActivity(intent);
                break;
            case R.id.ll_call_transfer:
                mDialog = new MyDialog(this, "提示","呼叫转移成功开启");
                mDialog.show();
                mDialog.positive.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mDialog.dismiss();
                    }
                });
                mDialog.negative.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mDialog.dismiss();
                    }
                });
                break;
            case R.id.ll_mail_list:
                intent = new Intent(this, MailListActivity.class);
                startActivity(intent);
                break;
            case R.id.ll_position_information:
                intent = new Intent(this, AreaActivity.class);
                startActivity(intent);
                break;
            case R.id.ll_set_up:
                intent = new Intent(this, SetActivity.class);
                startActivity(intent);
                break;
            case R.id.ll_sign_out:
                editor.putString("user_id", "");
                editor.commit();//提交修改
                Toast.makeText(this,"用户退出登录成功",Toast.LENGTH_SHORT).show();
                intent = new Intent(this, LoginActivity.class);
                startActivityForResult(intent, 0);
                break;
            default:
                break;
        }
    }

    private boolean isLogin(String user_id) {
        if (user_id.equals("")){
            Intent intent = new Intent(this, LoginActivity.class);
            startActivityForResult(intent,0);
            return false;
        }
        return true;
    }

    private boolean isExit = false;

    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            isExit=false;
        }
    };

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            if (isExit == false) {
                isExit = true;
                Toast.makeText(this, "再按一次退出APP", Toast.LENGTH_SHORT).show();
                handler.sendEmptyMessageDelayed(0, 3000);
            }else {
                finish();
                return false;
            }
        }
        return true;
    }
}
